package com.ruoyi.teach.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.teach.mapper.TeachBookContentMapper;
import com.ruoyi.teach.domain.TeachBookContent;
import com.ruoyi.teach.service.ITeachBookContentService;

/**
 * 教材内容Service业务层处理
 * 
 * @author yepanpan
 * @date 2021-03-15
 */
@Service
public class TeachBookContentServiceImpl implements ITeachBookContentService 
{
    @Autowired
    private TeachBookContentMapper teachBookContentMapper;

    /**
     * 查询教材内容
     * 
     * @param id 教材内容ID
     * @return 教材内容
     */
    @Override
    public TeachBookContent selectTeachBookContentById(Long id)
    {
        return teachBookContentMapper.selectTeachBookContentById(id);
    }

    /**
     * 查询教材内容列表
     * 
     * @param teachBookContent 教材内容
     * @return 教材内容
     */
    @Override
    public List<TeachBookContent> selectTeachBookContentList(TeachBookContent teachBookContent)
    {
        return teachBookContentMapper.selectTeachBookContentList(teachBookContent);
    }

    /**
     * 新增教材内容
     * 
     * @param teachBookContent 教材内容
     * @return 结果
     */
    @Override
    @Transactional
    public int insertTeachBookContent(TeachBookContent teachBookContent)
    {
    	updateCurrent(teachBookContent);
    	
        teachBookContent.setCreateTime(DateUtils.getNowDate());
        teachBookContent.setCreateBy(SecurityUtils.getUsername());
        return teachBookContentMapper.insertTeachBookContent(teachBookContent);
    }

    /**
     * 修改教材内容
     * 
     * @param teachBookContent 教材内容
     * @return 结果
     */
    @Override
    @Transactional
    public int updateTeachBookContent(TeachBookContent teachBookContent)
    {
    	updateCurrent(teachBookContent);
    	
        teachBookContent.setUpdateTime(DateUtils.getNowDate());
        teachBookContent.setUpdateBy(SecurityUtils.getUsername());
        return teachBookContentMapper.updateTeachBookContent(teachBookContent);
    }
    
    /**
     * 更新当前内容的状态，一个节点只能有一个当前内容
     * 如果新节点不是当前内容，则忽略
     * 如果原来没有当前内容，也忽略
     * 如果原来有当前内容。且新内容也是当前内容时，将原来的置非
     * 
     * @param teachBookContent
     */
    private void updateCurrent(TeachBookContent teachBookContent) {
    	if(!"Y".equals(teachBookContent.getIsCurrent())) return;
    	
    	TeachBookContent cc = new TeachBookContent();
    	cc.setBookId(teachBookContent.getBookId());
    	cc.setNodeId(teachBookContent.getNodeId());
    	cc.setIsCurrent("Y");
    	List<TeachBookContent> list = teachBookContentMapper.selectTeachBookContentList(cc);
    	if(list == null || list.size() == 0) return;
    	
    	TeachBookContent old = list.get(0);
    	old.setIsCurrent("N");
    	teachBookContentMapper.updateTeachBookContent(old);
    }

    /**
     * 批量删除教材内容
     * 
     * @param ids 需要删除的教材内容ID
     * @return 结果
     */
    @Override
    public int deleteTeachBookContentByIds(Long[] ids)
    {
        return teachBookContentMapper.deleteTeachBookContentByIds(ids);
    }

    /**
     * 删除教材内容信息
     * 
     * @param id 教材内容ID
     * @return 结果
     */
    @Override
    public int deleteTeachBookContentById(Long id)
    {
        return teachBookContentMapper.deleteTeachBookContentById(id);
    }
}
